<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="fromData" label-width="120px" style="margin-left: 120px; margin-top:30px" :model="fromData" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="fromData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="newPassword">
                <el-input v-model="fromData.newPassword" style="width:300px" show-password />
              </el-form-item>
              <el-form-item label="确定密码:" prop="verifyPassword">
                <el-input v-model="fromData.verifyPassword" style="width:300px" show-password />
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="saveEmployeeBasic">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <userInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <jobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import userInfo from './components/userInfo.vue'
import jobInfo from './components/jobInfo.vue'

import { getUserDetailById } from '@/api/user'
import { saveEmployeeBasic } from '@/api/employees'
export default {
  components: {
    userInfo,
    jobInfo
  },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === this.fromData.newPassword) callback()
      else callback(new Error('两次输入的密码不相同'))
    }
    return {
      fromData: {
        username: '',
        password: '',
        newPassword: '',
        verifyPassword: ''
      },
      rules: {
        username: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        verifyPassword: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  async mounted() {
    // 钩子函数
    this.fromData = await getUserDetailById(this.$route.params.id)
    // console.log(this.fromData)
  },
  methods: {
    // 点击更新按钮
    async saveEmployeeBasic() {
      try {
        await this.$refs.fromData.validate()
        this.fromData.password = this.fromData.newPassword
        await saveEmployeeBasic(this.fromData)
        this.$message.success('修改成功')
        this.fromData.verifyPassword = this.fromData.newPassword = ''
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style  scoped>
</style>
